/** 路由的相关设置 */
import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import Welcome from '../components/Welcome.vue'

// 路由懒加载
const Login = () => import(/* webpackChunkName: "login_home_welcom" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "login_home_welcom" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "login_home_welcom" */ '../components/Welcome.vue')

// import Users from '../components/user/Users.vue'
// import Rights from '../components/power/Rights.vue'
// import Roles from '../components/power/Roles.vue'

// 路由懒加载
const Users = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/user/Users.vue')
const Rights = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/power/Roles.vue')

// import Cate from '../components/goods/Cate.vue'
// import Params from '../components/goods/Params.vue'

// 路由懒加载
const Cate = () => import(/* webpackChunkName: "cate_params" */ '../components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName: "cate_params" */ '../components/goods/Params.vue')

// import List from '../components/goods/List.vue'
// import Add from '../components/goods/Add.vue'

// 路由懒加载
const List = () => import(/* webpackChunkName: "list_add" */ '../components/goods/List.vue')
const Add = () => import(/* webpackChunkName: "list_add" */ '../components/goods/Add.vue')

// import Order from '../components/order/Order.vue'
// import Report from '../components/report/Report.vue'

// 路由懒加载
const Order = () => import(/* webpackChunkName: "order_report" */ '../components/order/Order.vue')
const Report = () => import(/* webpackChunkName: "order_report" */ '../components/report/Report.vue')

Vue.use(VueRouter)

const routes = [
  // 重定向
  { path: '/', redirect: '/login' },
  // 登录组件路由规则
  { path: '/login', component: Login },
  // Home组件路由规则
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      // Welcome 欢迎页面路由规则
      { path: '/welcome', component: Welcome },
      // Users 用户管理页面路由规则
      { path: '/users', component: Users },
      // Rights 权限管理页面组件路由规则
      { path: '/rights', component: Rights },
      // Roles 角色管理页面组件的路由规则
      { path: '/roles', component: Roles },
      // Cate 商品分类页面组件的路由规则
      { path: '/categories', component: Cate },
      // Params 商品参数页面组件的路由规则
      { path: '/params', component: Params },
      // List 商品列表页面组件的路由规则
      { path: '/goods', component: List },
      // Add 添加商品页面组件的路由规则
      { path: '/goods/add', component: Add },
      // Order 添加商品页面组件的路由规则
      { path: '/orders', component: Order },
      // Report 添加商品页面组件的路由规则
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫, 每次跳转页面之前都会执行路由守卫函数
router.beforeEach((to, from, next) => {
  // 判断用户是否正在访问登录页
  if (to.path === '/login') return next()
  // 获取客户端保存的 token 值
  const tokenStr = window.sessionStorage.getItem('token')
  // 判断 token 值是否有效, 如果无效则访问登录页面
  if (!tokenStr) return next('/login')
  // token 值有效, 执行放行的函数
  next()
})

export default router
